package com.hankol.newretail.adapter.details

import android.view.View
import android.view.ViewGroup
import android.annotation.SuppressLint
import android.widget.TextView
import androidx.core.view.marginTop
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.Constants
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.extension.setMallPriceTTF
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.AmountUtil
import com.baselibrary.utils.NumberUtils
import com.baselibrary.utils.ScreenUtils
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.adapter.GoodsLabelAdapter
import com.hankol.newretail.bean.GoodListBean
import com.hankol.newretail.widget.NoSpaceTextView

/**
 * @description 详情，相关推荐
 * @author 关忆北、
 * @time 2022/8/18 8:09 下午
 * isShowCompleteLabel:true 完整显示标签个数，如商品详情，false：列表内最多展示3个标签
 */
class GoodsDetailsRecommendAdapter(val isShowCompleteLabel: Boolean = false) : BaseQuickAdapter<GoodListBean, BaseViewHolder>(R.layout.item_details_recommend_goods) {
    private var mOnItemClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(mOnItemClickListener: OnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: GoodListBean) {
        helper.apply {
            item.let {
                val tvRecommendName = getView<TextView>(R.id.tv_recommend_name)
                tvRecommendName.text = "${it.name.checkValue()}\n"
//                tvRecommendName.setText("${it.name.checkValue()}\n",TextView.BufferType.NORMAL)

                getView<RoundCornerImageView>(R.id.iv_recommend_cover).load(mContext, it.icon.checkValue())
                setText(R.id.tv_recommend_price, AmountUtil.changeF2Y("${it.showPrice}"))
                setText(R.id.tv_recommend_month_sell, "月销 ${NumberUtils.formatBigNum("${it.virtualPayPersonNum}")}+")
                setGone(R.id.tv_recommend_month_sell, it.virtualPayPersonNum != 0)
                getView<TextView>(R.id.tv_recommend_price).setMallPriceTTF(mContext)
                //标签
                val rvLabel = getView<RecyclerView>(R.id.rv_recommend_label)
                val labelAdapter = GoodsLabelAdapter()
                rvLabel.adapter = labelAdapter
                labelAdapter.setNewData(if (isShowCompleteLabel) it.mallLabelDtoList else if (it.mallLabelDtoList.size > 3) it.mallLabelDtoList.subList(0, 3) else it.mallLabelDtoList)
                //是否显示视频标识
                setGone(R.id.img_is_video, it.hasVideos == 1)
            }


            setGone(R.id.view_recommend_right_line, helper.adapterPosition % 2 == 0)
            if (helper.adapterPosition == 0) {
                val view = helper.getView<View>(R.id.view_recommend_right_line)
                var params: ViewGroup.LayoutParams = view.layoutParams

                var marginParams: ViewGroup.MarginLayoutParams? = null
                //获取view的margin设置参数
                marginParams = if (params is ViewGroup.MarginLayoutParams) {
                    params
                } else {
                    //不存在时创建一个新的参数
                    //基于View本身原有的布局参数对象
                    ViewGroup.MarginLayoutParams(params)
                }
                marginParams.setMargins(0, ScreenUtils.dpToPx(20), 0, 0)
            } else {
                val view = helper.getView<View>(R.id.view_recommend_right_line)
                var params: ViewGroup.LayoutParams = view.layoutParams

                var marginParams: ViewGroup.MarginLayoutParams? = null
                //获取view的margin设置参数
                marginParams = if (params is ViewGroup.MarginLayoutParams) {
                    params
                } else {
                    //不存在时创建一个新的参数
                    //基于View本身原有的布局参数对象
                    ViewGroup.MarginLayoutParams(params)
                }
                marginParams.setMargins(0, 0, 0, 0)
            }

            itemView.setOnClickListener {
                val hashMap: HashMap<String, Any> = hashMapOf()
                hashMap[Constants.GOODS_DETAIL_ID] = item.id.toLong()
                hashMap[Constants.GOODS_DETAIL_SOURCE] = "商品详情页推荐专区"
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, hashMap)
                mOnItemClickListener?.itemClickListener()
            }
        }
    }

    interface OnItemClickListener {
        fun itemClickListener()
    }

}